package com.tj.common;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
    /**
     * return a date range represents yesterday's range, inclusive.
     * @return
     */
    public static Date[] getLastDayDateRange() {
        Date[] r = new Date[2];
        Calendar cal = Calendar.getInstance(Locale.FRANCE);
        int day = cal.get(Calendar.DAY_OF_MONTH) - 1;
        cal.set(Calendar.DAY_OF_MONTH, day);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 1);
        r[0] = cal.getTime();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        r[1] = cal.getTime();
        return r;
    }

    public static Date[] getLastWeekDateRange() {
        Date[] r = new Date[2];
        Calendar cal = Calendar.getInstance(Locale.FRANCE);
        int week = cal.get(Calendar.WEEK_OF_YEAR) - 1;
        cal.set(Calendar.WEEK_OF_YEAR, week);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        r[0] = cal.getTime();

        // increase by 6, get sunday's date
        cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + 6);
        r[1] = cal.getTime();
        return r;
    }

    public static Date[] getLastMonthDateRange() {
        Date[] r = new Date[2];
        Calendar cal = Calendar.getInstance(Locale.FRANCE);

        cal.set(Calendar.DAY_OF_MONTH, 0);
        r[1] = cal.getTime();

        cal = Calendar.getInstance(Locale.CHINA);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        r[0] = cal.getTime();
        return r;
    }
}
